home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / powervww / pvcombo.cpp < prev    next >
C/C++ Source or Header  |  1998-01-05  |  9KB  |  442 lines

  1. //  ____________________________________________________
  2. // |                                                    |
  3. // |  Project:     POWER VIEW INTERFACE                 |
  4. // |  File:        PVCOMBO.CPP                          |
  5. // |  Compiler:    WPP386 (10.6)                        |
  6. // |                                                    |
  7. // |  Subject:     Combo-box implementation             |
  8. // |                                                    |
  9. // |  Author:      Emil Dotchevski                      |
  10. // |____________________________________________________|
  11. //
  12. // E-mail: zajo@geocities.com
  13. // URL:    http://www.geocities.com/SiliconValley/Bay/3577
  14.  
  15. #define uses_string
  16. #define uses_colors
  17. #define uses_combo
  18. #define uses_dc
  19. #define uses_dialog
  20. #define uses_icons
  21. #define uses_label
  22. #define uses_lines
  23. #define uses_system
  24.  
  25. #include "PVuses.h"
  26.  
  27. static Tcombo_list *combo_list = NULL;
  28. static Tcombo *combo = NULL;
  29.  
  30. //Tprompt_item publics:
  31.  
  32. Tprompt_item::Tprompt_item( char *_prompt, int _xl ):
  33.   Titem( _xl, 1 )
  34. {
  35.   prompt_len = 0;
  36.   if( ( _prompt != NULL ) && *_prompt )
  37.   {
  38.     prompt_len = (char) ( smart_len( _prompt ) + 1 );
  39.     put_in( NEW( Tlabel( _prompt, this ) ), -prompt_len, 0 );
  40.   }
  41. }
  42.  
  43. //Tcombo_item publics:
  44.  
  45. Tcombo_item::Tcombo_item( char *_prompt, int _xl ):
  46.   Tprompt_item( _prompt, _xl ),
  47.   Tlb_list()
  48. {
  49. }
  50.  
  51. Tcombo_item::Tcombo_item( char *_prompt, int _xl, Tlb_list *_list ):
  52.   Tprompt_item( _prompt, _xl ),
  53.   Tlb_list( _list )
  54. {
  55. }
  56.  
  57. //Tcombo_list publics:
  58.  
  59. Tcombo_list::Tcombo_list( uint &_data, int _xl, int _max_print, Tlb_list *_list ):
  60.   Tlist_box( _data, _xl, _max_print, _list )
  61. {
  62.   if( i_sb_up_len==1 ) v_bar->drag( v_bar->x - 1, v_bar->y );
  63.   set_state( isON_TOP, 1 );
  64.   at( _data );
  65. }
  66.  
  67. Tcombo_list::~Tcombo_list( void )
  68. {
  69.   combo_list = NULL;
  70. }
  71.  
  72. //Tcombo_list protected
  73.  
  74. void Tcombo_list::set_palette( void )
  75. {
  76.   Tlist_box::set_palette();
  77.   text_attr = pal_menus.normal;
  78.   selected_attr = pal_menus.selected;
  79.   disabled_attr = pal_menus.disabled;
  80. }
  81.  
  82. boolean Tcombo_list::release_focus( void )
  83. {
  84.   if( Tlist_box::release_focus() ) return combo_list == NULL;
  85.   return 0;
  86. }
  87.  
  88. void Tcombo_list::event_handler( Tevent &ev )
  89. {
  90.   Tlist_box::event_handler( ev );
  91.   switch( ev.code )
  92.   {
  93. #ifndef NOMOUSE
  94.     case evMOUSE_UP:
  95.       if( ev.INSIDE )
  96.       {
  97.         combo->set_index();
  98.         combo->close_combo();
  99.       }
  100.       handled( ev );
  101.       break;
  102.     case evMOUSE_DOWN:
  103.       if( !ev.INSIDE )
  104.       {
  105.         while( get_mouse(ev,0) );
  106.         combo->close_combo();
  107.       }
  108.     case evMOUSE_REP:
  109.       handled( ev );
  110.       break;
  111. #endif
  112.     case evKEY_PRESS:
  113.       switch( ev.ASCII )
  114.       {
  115.         case kESC:
  116.           handled( ev );
  117.           combo->close_combo();
  118.           break;
  119.         case kENTER:
  120.           handled( ev );
  121.           combo->set_index();
  122.           combo->close_combo();
  123.       }
  124.   }
  125. }
  126.  
  127. //Tcombo publics:
  128.  
  129. Tcombo::Tcombo( void ):
  130.   Ticon( i_combo_closed, 0, 0 )
  131. {
  132.   open_yl = __combo_lines();
  133.   open = 0;
  134. }
  135.  
  136. void Tcombo::open_combo( void )
  137. {
  138.   int a, b;
  139.  
  140.   if( open ) return;
  141.   set_title( i_combo_open );
  142.   combo_list = init_combo_list();
  143.   owner->owner->put_in( combo_list, owner->x + owner->xl - open_xl, owner->y + 1 );
  144.   combo_list->make_global( 0, combo_list->yl, a, b);
  145.   if( ( b + (char) ( combo_list->hsize > open_xl ) ) > scr_rows )
  146.   {
  147.     combo_list->drag( combo_list->x, owner->y - open_yl );
  148.     combo_list->h_bar->drag( i_sb_left_len, -1 );
  149.   }
  150.   combo = this;
  151.   open = 1;
  152.   redraw();
  153. }
  154.  
  155. void Tcombo::close_combo( void )
  156. {
  157.   Tcombo_list *c;
  158.  
  159.   if( open && ( combo_list != NULL ) )
  160.   {
  161.     set_title( i_combo_closed );
  162.     c = combo_list; combo_list = NULL;
  163.     owner->focus();
  164.     DELETE( c );
  165.     combo = NULL;
  166.     open = 0;
  167.     owner->owner->redraw();
  168.   }
  169. }
  170.  
  171. void Tcombo::set_index( void )
  172. {
  173.   ( (Tcombo_item *) owner )->set_data( combo_list->vcurrent );
  174. }
  175.  
  176. void Tcombo::get_index( void )
  177. {
  178.   char s[256];
  179.  
  180.   ( (Tcombo_item *) owner )->get_data( s );
  181.   ( (Tcombo_item *) owner )->add( s );
  182. }
  183.  
  184. //Tcombo protected:
  185.  
  186. void Tcombo::initialize( void )
  187. {
  188.   Ticon::initialize();
  189.   open_xl = owner->xl;
  190. }
  191.  
  192. void Tcombo::event_handler( Tevent &ev )
  193. {
  194.   if( owner->state( isFOCUSED ) && ( ev.code == evKEY_PRESS ) && ( ev.ASCII == kDOWN ) && !open )
  195.   {
  196.     open_combo();
  197.     redraw();
  198.     handled( ev );
  199.   }
  200.   else
  201.     Ticon::event_handler( ev );
  202. }
  203.  
  204. void Tcombo::press( void )
  205. {
  206.   if( open ) close_combo(); else open_combo();
  207. }
  208.  
  209. Tcombo_list * Tcombo::init_combo_list( void )
  210. {
  211.   return NEW( Tcombo_list(
  212.     ( (Tcombo_item *) owner )->vcurrent,
  213.     open_xl - i_sb_up_len,
  214.     open_yl,
  215.     (Tcombo_item *) owner)
  216.   );
  217. }
  218.  
  219. //Tcombo_box publics:
  220.  
  221. Tcombo_box::Tcombo_box( char *_prompt, uint &_data, int _xl ):
  222.   Tcombo_item( _prompt, _xl + 2 )
  223. {
  224.   init( _data );
  225. }
  226.  
  227. Tcombo_box::Tcombo_box( char *_prompt, uint &_data, int _xl, Tlb_list *_list ):
  228.   Tcombo_item( _prompt, _xl + 2, _list )
  229. {
  230.   init( _data );
  231. }
  232.  
  233. Tcombo_box::~Tcombo_box( void )
  234. {
  235.   if( data_txt != NULL ) FREE( data_txt );
  236. }
  237.  
  238. uint Tcombo_box::cursor( void )
  239. {
  240.   return data_num;
  241. }
  242.  
  243. void Tcombo_box::set_data( uint i )
  244. {
  245.   char s[256];
  246.  
  247.   gettxt( i, s );
  248.   if( data_txt != NULL ) FREE( data_txt );
  249.   data_txt = STRDUP( s );
  250.   data_num = i;
  251.   at( i );
  252.   item_acted = this;
  253. }
  254.  
  255. void Tcombo_box::get_data( char *s )
  256. {
  257.   strcpy( s, data_txt );
  258. }
  259.  
  260. //Tcombo_box protected:
  261.  
  262. void Tcombo_box::draw( void )
  263. {
  264.   char mp, ta;
  265.   char d[256];
  266.  
  267. #ifndef HGR
  268.   if( !graph_flag )
  269.   {
  270.     selected_attr = rolb( selected_attr, 4 );
  271.     text_attr = rolb( text_attr, 4 );
  272.     bold_attr &= 0x0F;
  273.     disabled_attr &= 0x0F;
  274.     bold_attr |= (char) ( text_attr & 0xF0 );
  275.     disabled_attr |= (char) ( text_attr & 0xF0 );
  276.   }
  277. #endif
  278.   mp = (char) ( xl - 2 );
  279.   direct_txt( i_left_line );
  280.   ta = text_attr;
  281.   if( state( isDISABLED ) )
  282.     text_attr = disabled_attr;
  283.   else
  284.     if( state( isSELECTED ) )
  285.       text_attr = bold_attr;
  286.   strcpy( d, data_txt );
  287.   d[mp] = 0;
  288.   direct_txt( d );
  289.   if( strlen( data_txt ) < mp ) mp -= strlen( data_txt ); else mp = 0;
  290.   txtf( "|r%c ", mp );
  291.   text_attr = ta;
  292.   direct_txt( i_right_line );
  293. }
  294.  
  295. void Tcombo_box::event_handler( Tevent &ev )
  296. {
  297.   Tcombo_item::event_handler( ev );
  298.   switch( ev.code )
  299.   {
  300.     case evCOMMAND:
  301.       switch( ev.CMD_CODE )
  302.       {
  303.         case cmDLG_RESET:
  304.           set_data( data_num );
  305.           break;
  306.         case cmLABEL_FOCUSED:
  307.           if( state( isFOCUSED ) )
  308.           {
  309.             combo->open_combo();
  310.             handled( ev );
  311.           }
  312.           break;
  313.       }
  314.       break;
  315.     case evMOUSE_UP:
  316.       if( ev.INSIDE && state(isFOCUSED) )
  317.       {
  318.         combo->open_combo();
  319.         handled( ev );
  320.       }
  321.       break;
  322.   }
  323. }
  324.  
  325. void Tcombo_box::ok_item( void )
  326. {
  327.   *dta = data_num;
  328. }
  329.  
  330. //Tcombo_box private:
  331.  
  332. void Tcombo_box::init( uint &_data )
  333. {
  334.   combo = NEW( Tcombo );
  335.   put_in( combo, xl, y );
  336.   data_txt = NULL;
  337.   data_num = _data;
  338.   dta = &_data;
  339.   put_in( NEW( Tline0( xl ) ), 0, 1 );
  340.   put_in( NEW( Tline1( xl ) ), 0, -1 );
  341. }
  342.  
  343. //PREFIXES
  344.  
  345. static int clines_ = 0;
  346.  
  347. /*
  348.   Description:
  349.     Specify open combo size. Call just before construct a combo item.
  350.   Entry:
  351.     cl - number of combo lines, default = 6.
  352. */
  353. void _combo_lines( int cl )
  354. {
  355.   if( !clines_ ) clines_ = cl;
  356. }
  357.  
  358. int __combo_lines( void )
  359. {
  360.   int result = 6;
  361.  
  362.   if( clines_ ) result = clines_;
  363.   clines_ = 0;
  364.   return result;
  365. }
  366.  
  367. //CONSTRUCTORS FOR USE WITH DIALOG BOXES
  368.  
  369. static void put_combo_box( Tcombo_box *c )
  370. {
  371.   hspaces( c->prompt_len );
  372.   put_item( c, c->xl + i_combo_open_len + 1, 2 );
  373.   if( !fill() ) hspaces( -c->prompt_len );
  374. }
  375.  
  376. /*
  377.   Description:
  378.     Construct combo box and insert it in the dialog box.
  379.   Entry:
  380.     t    - title, shortcut prefix '|~';
  381.     data - buffer that holds current selection;
  382.     _xl  - size of the combo box.
  383.   Exit:
  384.     Return pointer to the combo box.
  385. */
  386. Tcombo_box *combo_box( char *t, uint &data, int _xl )
  387. {
  388.   Tc